using System.Collections;
using System.IO;
using BasicScript;
using Game.Core;
using Game.Main.Model;
using UnityEngine;
using UnityEngine.UI;

namespace Game.Manager
{
	public class ConfigManager : Singleton<ConfigManager>
	{
		private int loadedNum;

		private int waitSign;

		private TextAsset[] txtScript;

		private int index;

		private int scriptNum;

		public override void Init()
		{
			loadedNum = 0;
			waitSign = 0;
			Client.Instance.StartCoroutine(StartLoadConfig());
		}

		private IEnumerator StartLoadConfig()
		{
            //txtScript = Resources.LoadAll<TextAsset>("prefabs/config");
            //StartParseConfig();
            //yield return null;
            //yield return null;
			ResourceRequest rqCompose = Resources.LoadAsync<TextAsset>("config/compose");
			yield return rqCompose;
			ResourceRequest rqItem = Resources.LoadAsync<TextAsset>("config/item");
			yield return rqItem;
			if (rqCompose.asset !=null&&rqItem.asset!=null)
			{
				txtScript = new TextAsset[]
				{
					rqCompose.asset as TextAsset,
					rqItem.asset as TextAsset,
				};
				scriptNum=txtScript.Length;
                StartParseConfig();
			}
			else
			{
                Debug.LogError("加载失败，请检查 Resources/config/ 下是否存在 compose 和 item 文件");
                if (rqCompose.asset == null)
                    Debug.LogError("compose 加载失败");
                if (rqItem.asset == null)
                    Debug.LogError("item 加载失败");
            }
        }

		private void StartParseConfig()
		{
			string text = txtScript[index].name + ".bytes";
			byte[] bytes = txtScript[index].bytes;
			byte[] data = bytes;
			data = DeEncrypt(data);
			ParserConfig(text.ToLower(), data);
			loadedNum++;
			waitSign++;
			index++;
			if (loadedNum >= scriptNum)
			{
				Fire(new BaseEvent("game.parse_config", new object[3] { true, loadedNum, scriptNum }));
			}
			else if (waitSign > 10)
			{
				if (Random.Range(0, 100) < 20)
				{
					waitSign = 0;
					Fire(new BaseEvent("game.parse_config", new object[3] { false, loadedNum, scriptNum }));
					Client.Instance.StartCoroutine(WaitParseXML());
				}
				else
				{
					StartParseConfig();
				}
			}
			else
			{
				StartParseConfig();
			}
		}

		private IEnumerator WaitParseXML()
		{
			yield return new WaitForSeconds(0.04f);
			StartParseConfig();
		}

		private void ParserConfig(string name, byte[] buffer)
		{
			MemoryStream memoryStream = new MemoryStream();
			memoryStream.Write(buffer, 0, buffer.Length);
			memoryStream.Position = 0L;
			if (name.Equals("item.bytes"))
			{
				Singleton<SceneModel>.Instance.ParseItemConfig(memoryStream);
			}
			else if (name.Equals("compose.bytes"))
			{
				Singleton<SceneModel>.Instance.ParseComposeConfig(memoryStream);
			}
			else
			{
				Debug.Log("\ufffd\ufffd\ufffdڶ\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffdļ\ufffd\ufffd\ufffd" + name);
			}
			memoryStream.Close();
			memoryStream.Dispose();
			memoryStream = null;
		}

		private byte[] DeEncrypt(byte[] data)
		{
			int num = 2;
			int num2 = data[num];
			data[num] = (byte)((uint)num2 ^ 8u);
			return data;
		}
	}
}
